
            


<div>
    <br><br>
    <?php echo lang('info_preview_template'); ?>

</div>
<br>
        <?php
	//print how much template(s) we get
	echo $count.' '.lang('general_record_found');
        ?>
<br>

<span class="btn" onClick="template_form()"><?php echo lang('menuitem_add_new')?></span>
<?php //echo anchor('', lang('menuitem_add_new'), 'class="btn" onClick="$(#div-form).show(); return false;"' )?>
<br>


<div id="div-form" style="display: none">
    <br>
    <form id="customForm" name="customForm" method="post" onSubmit="save_template();  return false;" action="#">
        <input type="hidden" id="id" name="template[id]" value=""/>
        <label><?php echo lang('menuitem_template').' '.lang('form_label_name') ?></label>
        <input type="text" name="template[name]" id="name" value="" />

        <label><?php echo lang('menuitem_template')?></label>
        <textarea name="template[template]" id="wysiwyg" class="tinymce"  rows="15" cols="70"> </textarea>

        <input type="submit" value="<?php echo lang('form_label_save')?>"/>

    </form>

</div>



        <?php
	if($count > 0) {
	?>

    <table  id='myTable' name='myTable' class='tablesorter' border='0' cellspacing='1' cellpadding='0'>
		<thead><tr>
			<th> # </th>
			<th> <?php echo lang('form_label_name')?> </th>
			<th> <?php echo lang('menuitem_edit')?> </th>
			<!-- <th> <?php echo lang('menuitem_delete')?> </th> -->
		</tr>
		</thead>
		<tbody>
    <?php
            $i = 0;
        foreach($info as $row){
            echo "<tr>";
            echo "<td>". ++$i ."</td>";
            echo "<td>".$row['name']."</td>";
            echo "<td> <a href='#div-form' class='' onClick='edit(".$row['id'].")'>".lang('menuitem_edit')."</a></td>";
            echo "<!--<td>". anchor("preview/delete_preview/".$row['id'] , lang('menuitem_delete'))."</td> -->";
            echo "</tr>";
        }
    echo "</table>";
    } //end if
    ?>



<!-- TinyMCE -->
<script type="text/javascript" src="<?php echo base_url()?>js/tiny_mce/jquery.tinymce.js"></script>
<script type="text/javascript">


	$().ready(function() {
		$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : '<?php echo base_url()?>js/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "pagebreak,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			//theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			//content_css : "css/content.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}

		});
	});




</script>
<!-- /TinyMCE -->




    <script type="text/javascript">

    function save_template()
    {
          $.post("<?php echo site_url()?>/preview/save_template", $("#customForm").serialize(),
            function(data){
             $("#div-form").hide('slow');
             alert(data);
              $("#id").val('')
              $("#name").val('')
              $('textarea.tinymce').tinymce().execCommand('new');
              location.reload();
         return false;
       });
            return false;
    }

    function edit(str)
    {

        $.post("<?php echo site_url()?>/preview/ajax_get_template_content/"+str+"/1",
		   function(data){
                       //alert(data.name)
                        $("#id").val(data.id)
                        $("#name").val(data.name)
                        $('iframe').get(0).contentWindow.document.body.innerHTML = data.template;
                        $('#div-form').show('slow')
	},'json');
    }

    function template_form()
    {
        $('iframe').get(0).contentWindow.document.body.innerHTML ='';
        $("#id").val('')
        $("#name").val('')
        $('#div-form').show('slow');
    }
    </script>

